$(function() {
    function initTimer() {
        $(".send-sms").addClass("disabled").html('重新发送(<span class="retry-time">60</span>)');

        var t = 59;
        var timer = setInterval(function() {
            if(!t) {
                $(".send-sms").removeClass("disabled").html("立即获取");
                clearInterval(timer);
            }
            $(".retry-time").html(t);
            t--;
        }, 1000);
    }

    $(".send-sms").on("click", function() {
        var phone = $("#phone").val();
        var phone_re = /^0\d{2,3}\d{7,8}$|^1[3578]\d{9}$|^147\d{8}$/;
        if(!phone_re.test(phone)) {
            $(".error-c").html("请输入正确的手机号码");
            return;
        }

        initTimer();

        $.ajax({
            url: '/api/user/sendsms',
            data: {
                postData: JSON.stringify({
                    idx: 1,
                    params:{
                        type: 'register',
                        phone: phone
                    }
                })
            },
            type: 'post',
            dataType: "json",
            success: function(res) {
                if(res.ret == 0) {
                    //alert("发送成功");
                } else {
                    alert(res.msg);
                }
            },
            error: function(res) {
                alert(res.msg);
            }
        });
    });

    $(".submit").on("click", function() {
        var phone = $("#phone").val();
        var passwd = $("#passwd").val();
        var verify = $("#verify").val();

        var phone_re = /^0\d{2,3}\d{7,8}$|^1[3578]\d{9}$|^147\d{8}$/;
        if(!phone_re.test(phone)) {
            $(".error-c").html("请输入正确的手机号码");
            return;
        }

        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                postData: JSON.stringify({
                    idx: 1,
                    params: {
                        phone: phone,
                        passwd: md5(passwd, "Sk4Ys7sPTx+gT5ssPHXV4ieKwPMKB0czjb+2rVfICMo="),
                        verify: verify
                    }
                })
            },
            success: function(res) {
                if(res.ret == 0) {
                    window.location.href="/web/signin"
                } else {
                    alert(res.msg);
                }
            },
            error: function(res) {
                alert(res.msg);
            }
        });
    });
});
